﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using AMPLibrary.Collections;
using AMPLibrary.Media;

namespace MediaLibrary.Controls
{
    /// <summary>
    /// Interaction logic for TagEditor.xaml
    /// </summary>
    public partial class AlbumTagEditorWindow : Window
    {
        private Collection<MediaFile> mediaToModify;

        /// <summary>
        /// 
        /// </summary>
        private AlbumTagEditorWindow()
        {
            InitializeComponent();
        }

        public AlbumTagEditorWindow(Collection<MediaFile> mediaToEdit)
        {
            InitializeComponent();

            if (mediaToEdit.Count == 0)
            {
                this.DialogResult = true;
                this.Close();
            }

            this.Topmost = true;
            this.mediaToModify = mediaToEdit;

            // album title
            if ((from m in mediaToModify select m.Tag.Album).Distinct().Count() > 1)
                this.albumTitleTextBox.Text = "Multiple Album Titles";
            else
                this.albumTitleTextBox.Text = (from m in mediaToModify select m.Tag.Album).ToArray()[0];

            // album artist
            if ((from m in mediaToModify select m.Tag.AlbumArtist).Distinct().Count() > 1)
                this.albumAlbumArtistTextBox.Text = "Multiple Album Artists";
            else
                this.albumAlbumArtistTextBox.Text = (from m in mediaToModify select m.Tag.AlbumArtist).ToArray()[0];

            // album genre
            if ((from m in mediaToModify select m.Tag.Genre).Distinct().Count() > 1)
                this.albumGenreTextBox.Text = "Multiple Genres";
            else
                this.albumGenreTextBox.Text = (from m in mediaToModify select m.Tag.Genre).ToArray()[0];

            // album year
            if ((from m in mediaToModify select m.Tag.Year).Distinct().Count() > 1)
                this.albumYearTextBox.Text = "Multiple Years";
            else
                this.albumYearTextBox.Text = (from m in mediaToModify select m.Tag.Year).ToArray()[0];

            // album conductor
            if ((from m in mediaToModify select m.Tag.Conductor).Distinct().Count() > 1)
                this.albumConductorTextBox.Text = "Multiple Conductors";
            else
                this.albumConductorTextBox.Text = (from m in mediaToModify select m.Tag.Conductor).ToArray()[0];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void editButton_Click(object sender, RoutedEventArgs e)
        {
            if (albumTitleTextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please provide an Album Name.", "Error", MessageBoxButton.OK);
                return;
            }

            if (albumAlbumArtistTextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please provide an Album Name.", "Error", MessageBoxButton.OK);
                return;
            }

            AMPLibrary.Media.MediaPlayer mediaPlayer = AMPLibrary.Media.MediaPlayer.GetMediaPlayer();
            mediaPlayer.StopForUpdate();

            foreach (MediaFile media in mediaToModify)
            {
                if (MediaTypes.IsValidMusicMediaType(media.Path))
                {
                    try
                    {
                        bool hasChanged = false;

                        if (albumTitleTextBox.Text.Trim() != "Multiple Album Titles")
                        {
                            media.Tag.Album = albumTitleTextBox.Text.Trim();
                            hasChanged = true;
                        }

                        if (albumAlbumArtistTextBox.Text.Trim() != "Multiple Album Artists")
                        {
                            media.Tag.AlbumArtist = albumAlbumArtistTextBox.Text.Trim();
                            hasChanged = true;
                        }     
                   
                        if(this.albumGenreTextBox.Text != "Multiple Genres")
                        {
                            if (media.Tag.Genre != this.albumGenreTextBox.Text.Trim())
                            {
                                media.Tag.Genre = this.albumGenreTextBox.Text.Trim();
                                hasChanged = true;
                            }
                        }

                        if (this.albumYearTextBox.Text != "Multiple Years")
                        {
                            if (media.Tag.Year != this.albumYearTextBox.Text.Trim())
                            {
                                media.Tag.Year = this.albumYearTextBox.Text.Trim();
                                hasChanged = true;
                            }
                        }

                        if (this.albumConductorTextBox.Text != "Multiple Conductors")
                        {
                            if (media.Tag.Conductor != this.albumConductorTextBox.Text.Trim())
                            {
                                media.Tag.Conductor = this.albumConductorTextBox.Text.Trim();
                                hasChanged = true;
                            }
                        }
                        
                        if(hasChanged)
                            media.Tag.WriteTagToDisk();
                    }
                    catch
                    {
                        MessageBox.Show("Could not update " + media.Path, "Error", MessageBoxButton.OK);
                    }
                }
            }

            mediaPlayer.ResumeFromUpdate();

            this.DialogResult = true;
            this.Close();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
